package cn.edu.gzhu.workOrderManagement.service.impl;

import cn.edu.gzhu.workOrderManagement.mapper.OrganizationMapper;
import cn.edu.gzhu.workOrderManagement.mapper.TeamMapper;
import cn.edu.gzhu.workOrderManagement.mapper.UserMapper;
import cn.edu.gzhu.workOrderManagement.pojo.dto.team.TeamCreateDto;
import cn.edu.gzhu.workOrderManagement.pojo.dto.team.TeamDeleteDto;
import cn.edu.gzhu.workOrderManagement.pojo.dto.team.TeamListDto;
import cn.edu.gzhu.workOrderManagement.pojo.dto.team.TeamUpdateDto;
import cn.edu.gzhu.workOrderManagement.pojo.entity.Organization;
import cn.edu.gzhu.workOrderManagement.pojo.entity.Team;
import cn.edu.gzhu.workOrderManagement.pojo.entity.User;
import cn.edu.gzhu.workOrderManagement.service.TeamService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

@Service
public class TeamServiceImpl implements TeamService {
    @Autowired
    TeamMapper teamMapper;
    @Autowired
    OrganizationMapper organizationMapper;
    @Autowired
    UserMapper userMapper;
    /**
     * @param teamCreateDto
     * @return
     */
    @Override
    public Boolean createTeam(TeamCreateDto teamCreateDto) {
        String originOrganization = teamCreateDto.getOriginOrganization();
        Organization organization = organizationMapper.selectOne(new LambdaQueryWrapper<Organization>().eq(Organization::getName, originOrganization));
        if (organization == null) {
            return false;
        }
        Team team = new Team();
        BeanUtils.copyProperties(teamCreateDto, team);
        team.setCreateTime(LocalDate.now());
        team.setUpdateTime(LocalDate.now());
        int insert = teamMapper.insert(team);
        return insert > 0;
    }

    /**
     * @param teamListDto
     * @return
     */
    @Override
    public IPage<Team> listTeam(TeamListDto teamListDto) {
        Page<Team> teamPage = new Page<>(teamListDto.getStartPage(), teamListDto.getPageSize());
        LambdaQueryWrapper<Team> queryWrapper = new LambdaQueryWrapper<>();
        if (teamListDto.getId() != null) {
            queryWrapper.like(Team::getId, teamListDto.getId());
        }
        if (teamListDto.getTeamName() != null) {
            queryWrapper.like(Team::getTeamName, teamListDto.getTeamName());
        }
        if (teamListDto.getOriginOrganization() != null) {
            queryWrapper.like(Team::getOriginOrganization, teamListDto.getOriginOrganization());
        }
        return teamMapper.selectPage(teamPage,queryWrapper);
    }

    /**
     * @param teamUpdateDto
     * @return
     */
    @Override
    public Boolean updateTeam(TeamUpdateDto teamUpdateDto) {
        Team team = new Team();
        BeanUtils.copyProperties(teamUpdateDto, team);
        team.setUpdateTime(LocalDate.now());
        int update = teamMapper.updateById(team);
        return update > 0;
    }

    /**
     * @param teamDeleteDto
     * @return
     */
    @Override
    public Boolean deleteTeam(TeamDeleteDto teamDeleteDto) {
        LambdaQueryWrapper<Team> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Team::getId, teamDeleteDto.getId());
        userMapper.delete(new LambdaQueryWrapper<User>().eq(User::getTeamId, teamDeleteDto.getId()));
        int deleteTeam = teamMapper.delete(queryWrapper);
        return deleteTeam > 0;
    }

}
